﻿using SW.Lite.Infrastructure.Common;
using SW.Lite.Infrastructure.Service;
using SW.Lite.Model;
using IModel = SW.Lite.IDAL.IModel;

namespace SW.Lite.BLL
{
    public class Model
    {
        private static readonly IModel Dal = ServiceLocator.Instance.GetInstance<IModel>("Model");

        /// <summary>
        ///     数据是否存在
        /// </summary>
        /// <param name="id">ID</param>
        public static bool Exists(int id)
        {
            return Dal.Exists(id);
        }

        /// <summary>
        ///     数据是否存在
        /// </summary>
        /// <param name="code">编码</param>
        public static bool Exists(string code)
        {
            return Dal.Exists(code);
        }

        /// <summary>
        ///     获取单个项
        /// </summary>
        /// <param name="id">ID</param>
        public static ModelModel Single(int id)
        {
            return Dal.Single(id);
        }

        /// <summary>
        ///     获取所有项目
        /// </summary>
        public static PageList<ModelModel> All()
        {
            return Dal.All();
        }

        /// <summary>
        ///     获取所有项目(分页)
        /// </summary>
        public static PageList<ModelModel> All(int pageIndex, int pageSize)
        {
            return Dal.All(pageIndex, pageSize);
        }

        /// <summary>
        ///     插入数据
        /// </summary>
        /// <param name="model">单个项</param>
        public static ExecuteResult Insert(ModelModel model)
        {
            var result = Dal.Insert(model);
            if (!result)
                return result;

            foreach (var field in model.Fields)
                Field.Insert(field);

            return result;
        }

        /// <summary>
        ///     更新数据
        /// </summary>
        /// <param name="model">单个项</param>
        public static ExecuteResult Update(ModelModel model)
        {
            return Dal.Update(model);
        }

        /// <summary>
        ///     删除数据
        /// </summary>
        /// <param name="id">ID</param>
        public static ExecuteResult Delete(int id)
        {
            return Dal.Delete(id);
        }

        /// <summary>
        ///     创建模型表
        /// </summary>
        /// <param name="model">模型</param>
        public static ExecuteResult CreateTable(ModelModel model)
        {
            return Dal.CreateTable(model);
        }
    }
}